package com.iotechn.unimall.admin.api.leaseCategory;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.LeaseCategoryDO;
import com.iotechn.unimall.data.domain.LeaseDO;
import com.iotechn.unimall.data.mapper.LeaseCategoryMapper;
import com.iotechn.unimall.data.mapper.LeaseMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminLeaseCategoryServiceImpl implements AdminLeaseCategoryService {

    @Autowired
    private LeaseCategoryMapper leaseCategoryMapper;
    @Autowired
    private LeaseMapper leaseMapper;

    @Override
    public Page<LeaseCategoryDO> list(Integer page, Integer limit, String name, Long adminId) throws ServiceException {
        Wrapper<LeaseCategoryDO> wrapper = new EntityWrapper<LeaseCategoryDO>();
        wrapper.setSqlSelect("id", "`name`");
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        wrapper.orderBy("id", false);
        Integer count = leaseCategoryMapper.selectCount(wrapper);
        List<LeaseCategoryDO> LeaseCategoryDOS = leaseCategoryMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(LeaseCategoryDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeaseCategoryDO create(LeaseCategoryDO leaseCategoryDO, Long adminId) throws ServiceException {
        Wrapper<LeaseCategoryDO> wrapper = new EntityWrapper<LeaseCategoryDO>()
                .eq("name", leaseCategoryDO.getName());
        if (leaseCategoryMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_PARTITION_CANNOT_REPEATED);
        if (leaseCategoryMapper.insert(leaseCategoryDO) > 0) {
            leaseCategoryDO.setId(leaseCategoryDO.getId());
            return leaseCategoryDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(LeaseCategoryDO leaseCategoryDO, Long adminId) throws ServiceException {
        Wrapper<LeaseCategoryDO> wrapper = new EntityWrapper<LeaseCategoryDO>()
                .eq("name", leaseCategoryDO.getName())
                .ne("id", leaseCategoryDO.getId());
        if (leaseCategoryMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_PARTITION_CANNOT_REPEATED);
        Long id = leaseCategoryDO.getId();
        if (id == null) return "fail";
        if (leaseCategoryMapper.updateById(leaseCategoryDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        Wrapper<LeaseDO> wrapper = new EntityWrapper<LeaseDO>().eq("category_id", id);
        if (leaseMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_CATEGORY_CANNOT_DELETE);
        if (leaseCategoryMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public LeaseCategoryDO detail(Long id, Long adminId) throws ServiceException {
        return leaseCategoryMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        Wrapper<LeaseDO> wrapper = new EntityWrapper<LeaseDO>().in("category_id", ids);
        if (leaseMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_CATEGORY_CANNOT_DELETE);
        if (leaseCategoryMapper.delete(new EntityWrapper<LeaseCategoryDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }

    @Override
    public List<LeaseCategoryDO> select(Long adminId) {
        Wrapper<LeaseCategoryDO> wrapper = new EntityWrapper<LeaseCategoryDO>().setSqlSelect("id", "name").orderBy("id", false);
        return leaseCategoryMapper.selectList(wrapper);
    }
}
